

#include<iostream.h>
#include<stdio.h>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

#include<vector>
using namespace std;
 
//char * Genid(FILE *);
char genid[15];




 char* Genid(FILE *f1)
 {
	 char title[50],*tittext;
      int in,titin,tflag=0;;
     char ch1;
	 ch1=fgetc(f1);

	 tittext=new char[30];
	 
	 while(true)
	 {
		 if(ch1=='<')
		 {
			 memset(title,'\0',50);
			 in=0;
			 while(ch1!='>')
			 {
				 title[in]=toupper(ch1);
				 in++;
				 ch1=fgetc(f1);
				 if(in>7)
					 break;

			 }
			 if(strcmp(title,"<TITLE")==0)
			 {
				 tflag=1;
			 }



		 }

		 if(tflag==1)
		 {
			 ch1=fgetc(f1);
			 memset(tittext,'\0',30);
			 titin=0;
			 while(ch1!='<')
			 {
				 tittext[titin]=toupper(ch1);
				 titin++;
				 ch1=fgetc(f1);


			 }
			 break;
					 


		 }
		 
		  ch1=fgetc(f1);
	 
	 }
	 return(tittext);




 }//end of function



 void parsef4row(FILE *fp1,FILE *fp2)
 {
	
	char gettr[10];
    memset(gettr,'\0',10);
    int in,cnttr;;
    bool firsttr;
    firsttr=false;
	char ch;
 
	
	//Get the first <tr>
   
    while(true)
    {

	   //search for <tr>
      
         
	   ch=fgetc(fp1);
	   
	
	   if(ch=='<')
	   {
		   in=0;
		   cnttr=0;
		   memset(gettr,'\0',10);

		   gettr[in]=toupper(ch);
		   in++;
		   
		   while(cnttr<=2)
		   {
			   ch=fgetc(fp1);
			   gettr[in]=toupper(ch);
			   in++;

			   if(strcmp(gettr,"<TR")==0)
			   {
				   ch=fgetc(fp1);
				   firsttr=true;
				   break;
			   }
			   cnttr++;
		   
		   }


	   }
	   if(firsttr==true)
	   {
		   cout<<"first <tr> found"<<endl;
		   break;
	   }



   }//end while


   //Get the Aliases text

 char extext[200];

 int tin,aliasflag=0;
 //int stextflag;
 int fndlessflag,firstflag;
   
   ch=fgetc(fp1);

   while(true)
   {
	   fndlessflag=0;
	 
       
	   if(ch=='<')
	   {
		  
		  while(ch!='>')
		    ch=fgetc(fp1);

		   
		   ch=fgetc(fp1);
		   
		   fndlessflag=1;

	    
	   
	   }

        while(ch=='\n' || ch==' ')
		{
		  ch=fgetc(fp1);
		}


		 //if no start tag is there after the closing tag extract the text till the start tag is found 
		 
		 if(ch!='<' && fndlessflag==1)
		 {
			 //ch=fgetc(fp1);
			 memset(extext,'\0',200);
			 tin=0;
			 
			 while(ch!='<')
			 {
				 firstflag=0;
			   
				 //loop until there is \n or space characters
			   
			   
				 while(ch=='\n' || ch==' ')
				 {
					firstflag=1; 
					ch=fgetc(fp1);
				 }
			   
				 
				
			     if(ch=='<')
				  break;

				 if(firstflag==1 && tin!=0)
				 {
				   extext[tin]='_';
				   tin++;
				 }

				 extext[tin]=toupper(ch);
				 tin++;

				if(strcmp(extext,"ALIASES_AND_ADDITIONAL_DESCRIPTION")==0)
				{
					aliasflag=1;					
					break;
				}

						
								
				ch=fgetc(fp1);



			 }//end of while
			 


		 }

		 if(aliasflag==1)
		 {
			 cout<<"Aliases text found"<<endl;
			 break;
		 }



		 if(fndlessflag==0)
		   ch=fgetc(fp1);


	   }//end of while true



  

    char mastertag[100];
    //char subtag[50];
	char gettd[100];
	int tdin;
	
   //search for </td>


	while(true)
	{
		
		if(ch=='<')
		{
			memset(gettd,'\0',100);
			tdin=0;
			
			while(ch!='>')
			{
		
			   while(ch=='\n' || ch==' ')
			   {
		         ch=fgetc(fp1);
			   }
				
				gettd[tdin]=toupper(ch);
				tdin++;
				
				ch=fgetc(fp1);


			}
			if(strcmp(gettd,"</TD")==0)
			{
				break;
			}


		}
		
		  //if(ch!='')
		  ch=fgetc(fp1);

	}//end while true

	
	strcpy(mastertag,extext);

    char aliastd[100];
	int alin,foundtd=0;


   //search for the tag <td>

  while(true)
  {

	  ch=fgetc(fp1);
		

	  if(ch=='<')
	  {
		
		    ch=fgetc(fp1);
		    memset(aliastd,'\0',100);
		    alin=0;

		   while(ch!='>')
		   {
			  firstflag=0;
			  		     
			 
			   while(ch=='\n' || ch==' ')
			   {
		         firstflag=1;
			     ch=fgetc(fp1);
			   }
			 
			   if(firstflag==1 && alin!=0)
			   { 
				 aliastd[alin]=' ';
				 alin++;
			   }
			 
			   aliastd[alin]=toupper(ch);
			   alin++;

			  if(strcmp(aliastd,"TD")==0)
			  {
				 foundtd=1;
				 //break;
			  }


               ch=fgetc(fp1);
			
		   }

	  }
	  
	  if(foundtd==1)
	  {
		   break;
	  }


   
  }//end of while true

 

  //search for </td> and extract all the aliases if it is there
   
    
  

    int aliascount,tagin;
	char tagtext[150],aliastext[200];

	
	int altin;
	ch=fgetc(fp1);

	aliascount=1;
	fprintf(fp2,"<%s>\n",mastertag);
	fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);
	 

     while(true)
	 {
		 
		 fndlessflag=0;

		 if(ch=='<')
		 {
			 fndlessflag=1;
			 memset(tagtext,'\0',100);
			 tagin=0;
			 while(ch!='>')
			 {
				 tagtext[tagin]=toupper(ch);
				 tagin++;
				 ch=fgetc(fp1);

			 }
			 ch=fgetc(fp1);
		
		
			 if(strcmp(tagtext,"</TD")==0)
				 break;

		 }
		 
		 
		 while(ch=='\n' || ch==' ')
		 { 
		   ch=fgetc(fp1);
		 }
		 
		 
		if(fndlessflag==1 && ch!='<')
		{

			 
			 memset(aliastext,'\0',200);
			 altin=0;
			 
			 while(ch!='<')
			 {
				 aliastext[altin]=toupper(ch);
				 altin++;
				 ch=fgetc(fp1);
				 
				 //if(ch=='-')
				  //noaliasdata=1;
			 }

			 			 
			 fprintf(fp2,"<alias%d>\n",aliascount);
			 fprintf(fp2," <value>%s</value>\n",aliastext);
			 fprintf(fp2,"</alias%d>\n",aliascount);
			 aliascount++;
                 
			 
		}
		 
		
		if(fndlessflag==0)
		  ch=fgetc(fp1);

	 }//end while true

	  fprintf(fp2,"</%s>\n\n",mastertag);

    

     //search for <TR> to parse the next row

	  /*while(ch=='\n' || ch==' ')
	  { 
		ch=fgetc(fp1);
	  }*/
	  
	  
      char extagtext[100];
	  int extagin,exmin;
	  char exmasttext[100];
      int firsttext,starttag;
	  int counttr=0;
      int firstenter=0; 
	  	
	  ch=fgetc(fp1);

	  while(true)
	  {
		  starttag=0;
		  fndlessflag=0;

		 
	      if(ch=='<')
		  {
			  memset(extagtext,'\0',100);
			  extagin=0;
			  while(ch!='>')
			  {
                  extagtext[extagin]=toupper(ch);
				  extagin++;
				  ch=fgetc(fp1);

			  }
			  
			  if(strcmp(extagtext,"<TR")==0)
			  {
				  firsttext=1;
				  starttag=1;
				  counttr++;
			  }


		  }//end of if


		  if(starttag==1)
		  {
			  //extract the text until the end tag is found

			 ch=fgetc(fp1);
			 			 
			 while(true)
			 {
				fndlessflag=0;
				 				 

				 if(ch=='<')
				 {
					 //fndlessflag=0;
					 memset(extagtext,'\0',100);
			         extagin=0;
					 while(ch!='>')
					 {
					   extagtext[extagin]=toupper(ch);
					   extagin=extagin+1;
					   ch=fgetc(fp1);
					 }
			
			 
			  //check whether </TD> has been found
                        
			      if(strcmp(extagtext,"</TD")==0)
				       break;
					 
					 ch=fgetc(fp1);
									
					 fndlessflag=1;

					 while(ch=='\n' || ch==' ')
				      ch=fgetc(fp1);
				 
				 }
				  
				  						

				  if(ch!='<' && fndlessflag==1)
				  {
					
					if(firsttext==1)
					{
					 memset(exmasttext,'\0',100);
					 exmin=0;
					}
						 

				  while(ch!='<')
				  {
					  

					  firstflag=0;

					  while(ch=='\n' || ch==' ')
					  {
						  firstflag=1;
						  ch=fgetc(fp1);
					  }

					 
					 if(firsttext==1)
					 {
					   if(firstflag==1 && exmin!=0)
					   {
						 exmasttext[exmin]='_';
						 exmin++;
					   }  

					   if(ch=='/')
					   {
						  exmasttext[exmin]='_';
						  exmin++;

					   }
					   else
					   {
					    exmasttext[exmin]=toupper(ch);
					    exmin++;
					   }
					 
					 }//end of if

					 ch=fgetc(fp1);

					 
			
				  }//end of while
				   
					firsttext=0;
					
					
				  }
				  if(fndlessflag==0)
					  ch=fgetc(fp1);
				
					
				
			 }

			 if(starttag==1)
			 {
			  fprintf(fp2,"<%s>\n",exmasttext);
			  fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);
			 }
			 

			 //get the field name and the text

			 int hypflag=0,bullhead=0;
			 
			 char hyperlink[350];
			 char exsubmasttext[150];
			 char exsubfield[150],exsubtext[900];
			 int extagin,hypin,exsfin,exstin;
			 int first=1,fndlessflag1;
			 
			 int firstprot=0;
			 int noenter=0;
			 int fields=0;
			 int bulloop;

             
			 *exsubmasttext='\0';
			 
			 memset(exsubfield,'\0',sizeof(exsubfield));
			 memset(exsubtext,'\0',sizeof(exsubtext));

			 exstin=0;
             

		    ch=fgetc(fp1);

		   while(true)
		   {

			   
			   fndlessflag1=0;
				//hypflag=0;
							

			   if(ch=='<')
			   {
				  memset(extagtext,'\0',200);
			      extagin=0;
					 
				  while(ch!='>')
				  {
					extagtext[extagin]=toupper(ch);
					extagin++;
					ch=fgetc(fp1);

					while(ch=='\n'  || ch==' ' || ch=='\t')
					  ch=fgetc(fp1);
					   
					   
					   
					//extract the hyperlink 
						
					if(strcmp(extagtext,"<AHREF=")==0)
					{
					   if(strcmp(exmasttext,"PROTEINS")==0)
					      firstprot=1;
							

						ch=fgetc(fp1);
						memset(hyperlink,'\0',350);
						hypin=0;
						hypflag=1;

						while(ch!='"')
						{
						  if(ch=='&')	
						  {
							 hyperlink[hypin]='$';
							hypin++;
						  }
						  else
						  {
							 hyperlink[hypin]=ch;
							 hypin++;
						  }
							
						   ch=fgetc(fp1);

						}
						//fprintf(fp2,"<href>%s</href>\n",hyperlink);
						

					   }//end of if
					   if(strcmp(extagtext,"<IMG")==0)
					   {
						 if(hypflag==1)
						 {
							fprintf(fp2,"<href>%s</href>\n",hyperlink);
							fprintf(fp2,"</%s>\n",exsubmasttext);
							hypflag=0;
						 }
						 memset(exsubmasttext,'\0',sizeof(exsubmasttext));

					   }

					 
					}//end of while

				  if(strcmp(extagtext,"<LI")==0)
				    bullhead=1;
					
				 if(strcmp(extagtext,"</TR")==0)
				 {
					if(*exsubmasttext!='\0')
					 fprintf(fp2,"</%s>\n",exsubmasttext);
					break;
				 }
							
					
				 fndlessflag1=1;						
				 ch=fgetc(fp1);
				 
				 while(ch=='\n' || ch==' ' || ch=='\t')
				    ch=fgetc(fp1);
					
				
			   }//end of if

			   				
			    
			       

			  if(ch!='<' && fndlessflag1==1)
			  {
				
				
				 
				 bulloop=0;
				 if(bullhead==1)
				 {
				     
					//if(first==1)//print the masttag when it enters the first time 
					  //fprintf(fp2,"<%s>\n",exsubmasttext);
					 
					 					 
					 first=0;
					 //fields=0;
			 				 
					 while(ch!='<')
					 {
						 firstflag=0;
						 
						 
						 while(ch=='\n' || ch==' ' || ch=='\t')
						 {
						    firstflag=1;
						    ch=fgetc(fp1);
						 }
						 
						if(ch==':')
						{
						   exsubtext[exstin]=ch;
						   exstin++;
							
						   exsfin=0;
						   exstin=0;
							
						   if(*exsubfield!='\0')
							fprintf(fp2,"</%s>\n",exsubfield);
							
						   memset(exsubfield,'\0',100);

							while(exsubtext[exsfin] !=':')
							{
							  if(exsubtext[exstin]==' ' || exsubtext[exstin]=='/')
								 exsubfield[exsfin]='_';
							  else
  							    exsubfield[exsfin]=exsubtext[exstin];
							
							  exsfin++;
							  exstin++;


							}//end while
							
							fprintf(fp2,"<%s>\n",exsubfield);

							
							fields=1;
							
							exstin=0;
						    memset(exsubtext,'\0',sizeof(exsubtext));
							
						}//end of if

						 
						if(firstflag==1 && exstin!=0)
						{
						  exsubtext[exstin]=' ';
						  exstin++;
						}
						
						if(firstflag==0 && ch!=':')
						{
						  exsubtext[exstin]=ch;
						  exstin++;
						}
						 
						
						if(strcmp(exsubtext,"&nbsp;")==0)
						{
						   memset(exsubtext,'\0',sizeof(exsubtext));
						   exstin=0;
						}
						
						
						if(firstflag==0)
						 ch=fgetc(fp1);

					   if(ch=='&')
					   {
						 char nbsp[8];
						 memset(nbsp,'\0',sizeof(nbsp));
						 int nbin=0;
						 while(strcmp(nbsp,"&nbsp")!=0)
						 {	
						   nbsp[nbin]=ch;
						   nbin++;
						   ch=fgetc(fp1);
						   if(ch!='n')
							 break;
						 }//end while
				
						 if(ch==';')
						  ch=fgetc(fp1);
						
					
					   }//end if

											
					 }//end of while
					 
					 
				   
				   if(fields==1 && *exsubtext!='\0')
				   {
					  					 
					 if(hypflag==1)
					 {
						fprintf(fp2,"<href>%s</href>\n",hyperlink);
						hypflag=0;
					 }
					 fprintf(fp2," <value>%s</value>\n",exsubtext);
					 exstin=0;
					 memset(exsubtext,'\0',sizeof(exsubtext));
					 
					 //fprintf(fp2,"</%s>\n",exsubfield);
					 bullhead=0;
					 fields=0;
					 
				   }//end if
				   
				   bulloop=1;
				   //bullhead=0;
				 						
						
				 }//end of if
				 
				 /*if(bullhead==0 && first==0 &&bulloop==0)
				 {
					fprintf(fp2,"</%s>\n",exsubmasttext);
					bullhead=0;
					first=1;

				 }*/
					
				int field;
				firstflag=0;
				
				if(bullhead==0 && bulloop==0)
				{
					

					while(ch!='<')
					{
						 
						
						field=0;
						 
					   						 
					   if(ch==':')
					   {
						   exsubtext[exstin]=ch;
					       exstin++;
						   
						   if(*exsubfield!='\0')
						   {
							 fprintf(fp2,"</%s>\n",exsubfield);
							 memset(exsubfield,'\0',sizeof(exsubfield));
						   }
						   
						   if(*exsubmasttext!='\0')
					         fprintf(fp2,"</%s>\n",exsubmasttext);

						   exsfin=0;
						   exstin=0;
						   memset(exsubmasttext,'\0',100);

						   while(exsubtext[exsfin] !=':')
						   {
							  
							 if(exsubtext[exstin]==' ' || exsubtext[exstin]=='/' || exsubtext[exstin]=='(' || exsubtext[exstin]==')')
							  exsubmasttext[exsfin]='_';
							 else
  							  exsubmasttext[exsfin]=exsubtext[exstin];
							  
							  exsfin++;
							  exstin++;


						   }//end of while
						   fprintf(fp2,"<%s>\n",exsubmasttext);

							
						   field=1;
							
						   exstin=0;
						   memset(exsubtext,'\0',sizeof(exsubtext));
						 	
					   
					   }//end of if

						 
					   if(firstflag==1 && exstin!=0)
					   {
						  exsubtext[exstin]=' ';
						  exstin++;
						}
						
						if(field==0 && firstflag==0)
						{
						 						   
						  if(!(ch=='[' || ch==']' || ch=='&'))
						  {
							exsubtext[exstin]=ch;
						    exstin++;
						  }
					   

						}//end of if
						
						 
						if(strcmp(exsubtext,"&nbsp")==0)
						{
						  memset(exsubtext,'\0',sizeof(exsubtext));
						  exstin=0;
						  ch=fgetc(fp1);
						}

					   if(ch=='&')
					   {
						 char nbsp[8];
						 memset(nbsp,'\0',sizeof(nbsp));
						 int nbin=0;
						 while(!(strcmp(nbsp,"&nbsp")==0 ||strcmp(nbsp,"&plusmn")==0))
						 {	
						   nbsp[nbin]=ch;
						   nbin++;
						   ch=fgetc(fp1);
						 }//end while
				
						 firstflag=1;
						  if(ch==';')
						  {
						    ch=fgetc(fp1);
							
						  }
						
						 if(strcmp(nbsp,"&plusmn")==0)
						 {
						   exsubtext[exstin]='+';
						   exstin++;
						   exsubtext[exstin]='/';
						   exstin++;
						   exsubtext[exstin]='-';
						   exstin++;
						 }//end if

					   }//end if
																		 
					  
					    if(firstflag==0)
						 ch=fgetc(fp1);
						 
					    firstflag=0;

					    while(ch=='\n' || ch==' ' || ch=='\t')
						{
						  firstflag=1;
						  ch=fgetc(fp1);
						}//end while
						 
						 				   
					
						
					}//end of while

				    if(strcmp(exmasttext,"PROTEINS")==0 && firstprot==0)
					{
					 noenter=1;//this variable is set if in Proteins the first text is not a hyperlink
					}
						

				 
				    if(*exsubtext!='\0')
					{
					 
					 if(strcmp(exmasttext,"PROTEINS")!=0 ||noenter==1)
					 {
					    
					  if(hypflag==1)
					  {
						fprintf(fp2,"<href>%s</href>\n",hyperlink);
						hypflag=0;
					  }
					  fprintf(fp2,"<value>%s</value>\n",exsubtext);
					
					 }//end if
					 
					 				 
				    if(strcmp(exmasttext,"PROTEINS")==0 && hypflag==1 && noenter==0)
					{
						 
				      fprintf(fp2,"<%s>\n",exsubtext);
					  strcpy(exsubmasttext,exsubtext);
						
					  if(hypflag==1)
					  {
					   fprintf(fp2,"<href>%s</href>\n",hyperlink);
					   hypflag=0;
					  }
					
					 //hypflag=0;
					 noenter=1;
				   }//end if
					 
					 /*if(hypflag==1)
					 {
						 fprintf(fp2,"<href>%s</href>\n",hyperlink);
						 hypflag=0;
					 }*/
					 					 					 

					exstin=0;
					memset(exsubtext,'\0',sizeof(exsubtext));
				  
				  }//end of if


				}//end of if
									
						
					
			}//end of if
				  
				
			 if(fndlessflag1==0)
				ch=fgetc(fp1);

				
			}//end of while true
			
			//print the end of the master tag
			fprintf(fp2,"</%s>\n\n",exmasttext);

           //fclose(fp2);


		  
		  }//end of if

		  
       if(counttr==4)
	   { 
		 break;
	   }


	   if(fndlessflag==0)
		ch=fgetc(fp1);

     }//end of while true
 

}//end function



 void parseexpr(FILE *fp1,FILE *fp2)
 {
	 
	  //find <tr>

	 char tagtext[200],hyptext[200],hyptextim[200],masttext[150];
	 char extext[150];
	 int exin;
	 int tagin,hypin,hypim;
	 char ch;
	 int masin=0;
	 int firstflag,masflag=0;
	 int endtag=0;
	 int hypflagim=0;
	 int hypflag=0;

   while(true)
   {
	  ch=fgetc(fp1);

	 if(ch=='<')
	 {
		memset(tagtext,'\0',200);
		tagin=0;

		while(ch!='>')
		{
		   tagtext[tagin]=toupper(ch);
		   tagin++;
		   ch=fgetc(fp1);
 
		}
		if(strcmp(tagtext,"<TR")==0)
		{
		  break;
		}
	 
	 }
   }


   int fndlflag=0;
	
   memset(masttext,'\0',sizeof(masttext));
   masin=0;
   
   while(true)
	{
		
		  //extract the master text
		  
	   if(fndlflag==0) 
	    ch=fgetc(fp1);
	   
	   while(ch=='\n'||ch==' '|| ch=='\t')
		  ch=fgetc(fp1);
		 		 
	    fndlflag=0;

	     if(ch=='<')
		 {
			 memset(tagtext,'\0',200);
			 tagin=0;

			 while(ch!='>')
			 {
		 	    tagtext[tagin]=toupper(ch);
				tagin++;
				ch=fgetc(fp1);
			 }
			 
			 if(strcmp(tagtext,"</TD")==0)
				 break;


			  ch=fgetc(fp1);
			  
			  while(ch=='\n'||ch==' '|| ch=='\t')
				  ch=fgetc(fp1);

			  fndlflag=1;

		  }




		 if(ch!='<')
		 {
			
					 
			while(ch!='<')
			{
			   firstflag=0;

			  while(ch=='\n' || ch==' '|| ch=='\t')
			  {
			   	ch=fgetc(fp1);
				firstflag=1;

			  }
			  if(masflag==0)
			  {
				if(firstflag==1 && masin!=0)
				{
					masttext[masin]='_';
					masin++;

				}
				else
				{
					masttext[masin]=toupper(ch);
					masin++;
				}
			  
			  }//end of if

			  if(firstflag==0)
				ch=fgetc(fp1);


			}//end of while

			masflag=1;
		 
		 }//end of if
				 

	}//end of while true

		
	fprintf(fp2,"<%s>\n",masttext);
	//fprintf(fp2,"<gid>%s</gid>\n",genid);
	fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);
	//extract the fields and text
		
	fndlflag=0;
	int table=0;

	while(true)
	{
	   if(fndlflag==0)
		ch=fgetc(fp1);

	   while(ch=='\n'||ch==' '|| ch=='\t')
		   ch=fgetc(fp1);
	   
	   if(ch=='<')
	   {
		   
		  memset(tagtext,'\0',200);
		  tagin=0;
		   
		   
		  while(ch!='>')
		  {
			  tagtext[tagin]=toupper(ch);
		      tagin++;
		      ch=fgetc(fp1);
			  			   
			  while(ch=='\n' || ch==' '|| ch=='\t')
			  {
				 ch=fgetc(fp1);
			  }
			  
			  if(strcmp(tagtext,"</TR")==0)
			  {
				  endtag++;
				  break;
			  }
			  if(strcmp(tagtext,"<TABLE")==0)
			  {
				  table=1;
			  }

			  if(strcmp(tagtext,"<AHREF=")==0)
			  {
				 ch=fgetc(fp1);
				 while(ch==' ' || ch=='\n' ||ch=='\t')
				   ch=fgetc(fp1);

				 memset(hyptext,'\0',200);
				 hypin=0;

				 while(ch!='"')
				 {
				  if(ch=='&')
				  {
				    hyptext[hypin]='$';
				    hypin++;
				  }
				  else
				  {
					hyptext[hypin]=ch;
				    hypin++;

				  }
					  
				   ch=fgetc(fp1);

				   while(ch=='\n' || ch==' '|| ch=='\t')
				   {
				      ch=fgetc(fp1);
				   }



				 }//end of while
				 //fprintf(fp2,"<href>%s</href>\n",hyptext);
                 hypflag=1;
				

			  }//end of if
			  			  
			  if(strcmp(tagtext,"<IMGSRC=")==0)
			  {
				 ch=fgetc(fp1);
				 while(ch==' ' || ch=='\n' ||ch=='\t')
				   ch=fgetc(fp1);

				 memset(hyptextim,'\0',200);
				 hypim=0;

				 while(ch!='"')
				 {
				  
				  if(ch=='&')
				  {
				    hyptextim[hypim]='$';
				    hypim++;
				  }
				  else
				  {
					hyptextim[hypim]=ch;
				    hypim++;

				  }
					  
				   ch=fgetc(fp1);

				   while(ch=='\n' || ch==' '|| ch=='\t')
				   {
				      ch=fgetc(fp1);
				   }


				 }//end of while
				 fprintf(fp2,"<image>%s</image>\n",hyptextim);

				 hypflagim=0;
				 hypflag=0;
				 //if(hypflag==1)



			  }//end of if



		  }//end of while
		  if(endtag==1 && table==0)
			break;

		  if(endtag==2 && table==1)
		   break;

		  ch=fgetc(fp1);
		  
		  while(ch=='\n'||ch==' '|| ch=='\t')
			 ch=fgetc(fp1);
		  
		  fndlflag=1;



	   }
	   
	    
	   if(ch!='<')
	   {

		   memset(extext,'\0',150);
		   exin=0;
		   while(ch!='<')
		   {
			   //neglect any nbsps
			 if(ch=='&')
			 {
			    char nbsp[7];
				memset(nbsp,'\0',sizeof(nbsp));
				int nbin=0;
				while(strcmp(nbsp,"&nbsp")!=0)
				{
				  nbsp[nbin]=ch;
				  nbin++;
				  ch=fgetc(fp1);
				}//end while
				
				if(ch==';')
				 ch=fgetc(fp1);

			 }//end if
			   
			  if(ch!='&')
			  {
			   extext[exin]=ch;
			   exin++;
			   ch=fgetc(fp1);
			  }
		       


		   }//end while
		   
		   if(hypflag==1)
			  fprintf(fp2,"<href>%s</href>\n",hyptext);

		   fprintf(fp2,"<value>%s</value>\n",extext);
		   
		   hypflag=0;
		   hypflagim=0;
			
			
	   }//end of if
		
		
		
	}//end of while true
	 
	 fprintf(fp2,"</%s>\n\n",masttext);

	 //fprintf(fp2,"</GENE>\n");



 }//end of function





 void parsesimgene(FILE *fp1,FILE *fp2)
 {

	char masttext[100],ch;
	char tagtext[150];
	int tagin,trcount=0,fndlflag;
    int masin,firstflag,firsttext=0;
	int tabl=0;
	 
    fndlflag=0;
	memset(masttext,'\0',sizeof(masttext));


	while(true)
	{
	  
	   if(fndlflag==0)
	    ch=fgetc(fp1);

	   if(ch=='<')
	   {
		  memset(tagtext,'\0',100);
		  tagin=0;

		  while(ch!='>')
		  {
			tagtext[tagin]=toupper(ch);
			tagin++;
			ch=fgetc(fp1);

		  

		   if(strcmp(tagtext,"<TR")==0)
			  trcount++;
		   if(strcmp(tagtext,"<TABLE")==0)
		   {
			  tabl=1;
		   }
		  
		   //if(trcount==4)
	         //break;
		  }//end while
		   
		   if(tabl==1)
			 break;
		  
		   fndlflag=1;
		   ch=fgetc(fp1);

		   while(ch=='\n'||ch==' '||ch=='\t')
		     ch=fgetc(fp1);

	   }//end of if
	   
	 

	 if(ch!='<' && trcount>=1)
	 {
		
		masin=0;
				
		while(ch!='<')
		{
		 firstflag=0;

		 while(ch=='\n' || ch==' '|| ch=='\t')
		 {
		   ch=fgetc(fp1);
		   firstflag=1;

		 }
			
			 
		if(firsttext==0)
		{
		 if(firstflag==1 && masin!=0)
		 {
		   masttext[masin]='_';
		   masin++;

		 }
		 else
		 {
		   masttext[masin]=toupper(ch);
		   masin++;
		 }
		}
			  
			  

		 if(firstflag==0)
		    ch=fgetc(fp1);
		
		}//end of while
		firsttext=1;
							

	 }//end of if
				
					
	}//end of while true

    fprintf(fp2,"<%s>\n",masttext);
    fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);


   
   //extract all the field values of similar genes

   char subfield[100],subtext[300],hyptext[150];
   int counttd=0,subin,hypin,tdflag=0,hypflag=0;
   memset(subfield,'\0',sizeof(subfield));


   int value=0;
   while(true)
   {
		
	  ch=fgetc(fp1);
		
	  while(ch=='\n'||ch==' '|| ch=='\t')
	   ch=fgetc(fp1);

					
	  if(ch=='<')
	  {
		 memset(tagtext,'\0',100);
		 tagin=0;
		  
		 while(ch!='>')
		 {
		   tagtext[tagin]=toupper(ch);
	       tagin++;
		   ch=fgetc(fp1);

		 }
		 if(strcmp(tagtext,"<TD")==0)
		 {
		   		
		   tdflag=1;
		   counttd++;
			
		   /*switch(counttd)
		   {
		     case 1:
				 memset(subfield,'\0',100);
				 subin=0;
				 break;

		     case 2:
				fprintf(fp2,"<Gene>\n");
				value=1;
				break;
			 case 3:
				 fprintf(fp2,"<Locus>\n");
				 value=1;
				 break;
			 case 4:
				 fprintf(fp2,"<Description>\n");
				 value=1;
				 break;
			 case 5:
				 fprintf(fp2,"<Similarity_to_human_gene>\n");
				 value=1;
				 break;
			 case 6:
				 fprintf(fp2,"<Genbank_accession>\n");
				 value=1;
				 break;

		   }//end of switch*/
		  
		 }//end of if
		 
		 if(strcmp(tagtext,"<TR")==0)
		   trcount++;

		 if(strcmp(tagtext,"</TR")==0)
		 {
			//print the submast tag of similar genes
			if(*subfield!='\0')
			 fprintf(fp2,"</%s>\n",subfield);
			counttd=0;
			tdflag=0;
		 }

		 //check for the end tag </table>
		 if(strcmp(tagtext,"</TABLE")==0)
		 {
			 //end of the row
			 fprintf(fp2,"</%s>\n",masttext);
			 break;
		 }		
			
	  }//end of if



	  		
	   if(tdflag==1 && trcount>=4)
	   {
           //print the field names of similar gene
		   switch(counttd)
		   {
		     case 1:
				 memset(subfield,'\0',100);
				 subin=0;
				 break;

		     case 2:
				fprintf(fp2,"<Gene>\n");
				value=1;
				break;
			 case 3:
				 fprintf(fp2,"<Locus>\n");
				 value=1;
				 break;
			 case 4:
				 fprintf(fp2,"<Description>\n");
				 value=1;
				 break;
			 case 5:
				 fprintf(fp2,"<Similarity_to_human_gene>\n");
				 value=1;
				 break;
			 case 6:
				 fprintf(fp2,"<Genbank_accession>\n");
				 value=1;
				 break;

		   }//end of switch

		 fndlflag=0;
		 while(true)
		 {
		
			if(fndlflag==0)
			 ch=fgetc(fp1);
			
			while(ch=='\n'||ch==' '|| ch=='\t')
			 ch=fgetc(fp1);
			fndlflag=0;
			
			if(ch=='<')
			{
			  memset(tagtext,'\0',150);
		      tagin=0;
			  while(ch!='>')
			  {
			    tagtext[tagin]=toupper(ch);
			    tagin++;
			    ch=fgetc(fp1);
				 
				while(ch=='\n'||ch==' '|| ch=='\t')
				  ch=fgetc(fp1);

				if(strcmp(tagtext,"<AHREF=")==0)
				{
					memset(hyptext,'\0',150);
					hypin=0;
					ch=fgetc(fp1);
					hypflag=1;

					while(ch!='"')
					{
					  if(ch=='&')
					  {
						 hyptext[hypin]='$';
						 hypin++;

					  }
					  else
					  {
					    hyptext[hypin]=ch;
					    hypin++;
					    
					  }
					  ch=fgetc(fp1);

					}
					//fprintf(fp2,"<href>%s</href>\n",hyptext);
				
				}//end of if
				
				
				
			  }//end of while

			  if(strcmp(tagtext,"</TD")==0)
			  {
				
				if(counttd==1)
				 fprintf(fp2,"<%s>\n",subfield);
				else
				{
					switch(counttd)
					{
					  case 2:
						fprintf(fp2,"</Gene>\n");
						break;
					  case 3:
				        fprintf(fp2,"</Locus>\n");
				         break;
			          case 4:
				       fprintf(fp2,"</Description>\n");
				       break;
			          case 5:
				       fprintf(fp2,"</Similarity_to_human_gene>\n");
				       break;
			          case 6:
				       fprintf(fp2,"</Genbank_accession>\n");
				       break;
											
					}//end of switch
				
				}//end else
				
			  break;
			

			  }//end if
			  
			  ch=fgetc(fp1);
			  fndlflag=1;
			  

			  
			  while(ch=='\n'|| ch==' '|| ch=='\t')
				 ch=fgetc(fp1);
			  

			}//end of if
			
			
			
		if(ch!='<')
		{
					    
		    memset(subtext,'\0',sizeof(subtext));

			  
		   if(counttd!=1)
			   subin=0;
		   while(ch!='<')
		   {
			if(counttd==1)
			{
			  if(ch==')'|| ch=='('|| ch==' ')
			  {
				subfield[subin]='_';
				subin++;
			  }
			  else
			  {
			    subfield[subin]=ch;
				subin++;
			  }

			}
			else
			{
			   subtext[subin]=ch;
			   subin++;
			}

			if(strcmp(subtext,"&nbsp;")==0)
			{
				memset(subtext,'\0',sizeof(subtext));
				subin=0;

			}

			 ch=fgetc(fp1);
				
		   }//end of while
			
				  
		   if(counttd>1)
		   {
			 if(*subtext!='\0')
			 {
			     
				fprintf(fp2,"<val%d>\n",value);
				if(hypflag==1)
				{
				  fprintf(fp2,"<href>%s</href>\n",hyptext);
				  hypflag=0;
				}
				fprintf(fp2,"<value>%s</value>\n",subtext);
				fprintf(fp2,"</val%d>\n",value);

				value++;
			 }
		   }
		   fndlflag=1;
			  
			
                 								
		}//end of if
			
			
	 }//end of while true




   }//end of if
		
		

  }//end of while true



  }//end of function




 void parseSnp(FILE *fp1,FILE *fp2)
 {

	 char tagtext[150],ch,masttext[100];
	 int tagin,masin;
     int fndlflag,firstflag,firsttext=0;
			
	 memset(masttext,'\0',sizeof(masttext));

	fndlflag=0; 
	while(true)
	{
		
	  if(fndlflag==0) 
	  	ch=fgetc(fp1);
	  
	  while(ch=='\n'|| ch==' '||ch=='\t')
		ch=fgetc(fp1);

	    fndlflag=0;
	   if(ch=='<')
	   {
		   memset(tagtext,'\0',100);
		   tagin=0;
		  while(ch!='>')
		  {
			tagtext[tagin]=toupper(ch);
			tagin++;
			ch=fgetc(fp1);
			
			while(ch=='\n'|| ch==' '||ch=='\t')
			 ch=fgetc(fp1);

		  }
		  if(strcmp(tagtext,"</TD")==0)
			 break;
		
		  ch=fgetc(fp1);
		  
		  while(ch=='\n'|| ch==' '||ch=='\t')
			ch=fgetc(fp1);

		  fndlflag=1;
		
		
	   }//end of if


				
     if(ch!='<')
	 {
		 fndlflag=1;
      		 
		 masin=0;

		while(ch!='<')
		{
			firstflag=0;

			while(ch=='\n' || ch==' '|| ch=='\t')
			{
		      ch=fgetc(fp1);
		      firstflag=1;

			}
			
			//extract the master text 
		  if(firsttext==0)
		  {
		    if((firstflag==1 && masin!=0) || ch=='/')
			{
		      masttext[masin]='_';
		      masin++;

			}
		    else
			{
		     masttext[masin]=toupper(ch);
		     masin++;
			}
		  }
			  
		  //if(firstflag==0)
		    ch=fgetc(fp1);


		}//end of while
		firsttext=1;
	 
	 }//end of if

		  
	}//end of while

		
	fprintf(fp2,"<%s>\n",masttext);
	fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);
	

    //extract the fields
	int tablflag=0,prevfield=0,hypflag,hypin;
	char hyptext[150],subtext[300],subfield[150];
    int subin=0,endtag=0;
	fndlflag=0;
	memset(subtext,'\0',sizeof(subtext));
	memset(subfield,'\0',150);

	while(true)
	{
	  if(fndlflag==0)
	   ch=fgetc(fp1);
		

	  fndlflag=0;

	  if(ch=='<')
	  {
		  memset(tagtext,'\0',150);
		  tagin=0;
		   
		 while(ch!='>')
		 {
		   tagtext[tagin]=toupper(ch);
		   tagin++;
		   ch=fgetc(fp1);
		   
		   while(ch=='\n' || ch==' '|| ch=='\t')
			 ch=fgetc(fp1);

		   if(strcmp(tagtext,"<TABLE")==0)
		   {
			   //fprintf(fp2,"</%s>\n",subfield);
			   tablflag=1;

		   }
		   if(strcmp(tagtext,"</TR")==0)
		   {
			  fprintf(fp2,"</%s>\n",subfield);			   				
			  endtag=1;
		   }

		   
		   if(strcmp(tagtext,"<AHREF=")==0)
		   {
			   ch=fgetc(fp1);
			   hypin=0;
			   memset(hyptext,'\0',150);
			   
			   while(ch=='\n' || ch==' '||ch=='\t')
			    ch=fgetc(fp1);
			   while(ch!='"')
			   {
				  hyptext[hypin]=ch;
				  hypin++;
				  ch=fgetc(fp1);

				 while(ch=='\n'|| ch==' '||ch=='\t')
			       ch=fgetc(fp1);

			   }
			   
			   hypflag=1;
		   }//end if

		  
		 }//end of while
		 
		 if(endtag==1)
		  break;

		 ch=fgetc(fp1);
		 
		 while(ch=='\n'|| ch==' '||ch=='\t')
		  ch=fgetc(fp1);
		  
		 fndlflag=1;


	  }//end of if


	  

	  if(tablflag==1)
		break;


	  if(ch!='<')
	  {
		  //memset(subtext,'\0',150);
		  //subin=0;
		  firstflag=0;		  	
		  while(ch!='<')
		  {
					 			   
			 if(firstflag==1 && subin!=0)
			 {
			   subtext[subin]=' ';
			   subin++;
			 }

			 			 
			  subtext[subin]=ch;
			  subin++;
						   
			 if(ch==':' && prevfield==0)
			 {
				 int tsubin=0;
				 int sfin=0;
				 if(*subfield!='\0')
				   fprintf(fp2,"</%s>\n",subfield);

				 memset(subfield,'\0',sizeof(subfield));
				  
				 while(subtext[tsubin]!=':')
				 {
				  
				    if(subtext[tsubin]==' ')
					{
					   subfield[sfin]='_';
				       sfin++;
				       tsubin++;
					}
					else
					{
					 subfield[sfin]=subtext[tsubin];
				     sfin++;
				     tsubin++;
					}

				 }
				 
				 memset(subtext,'\0',sizeof(subtext));
				 subin=0;
				 fprintf(fp2,"<%s>\n",subfield);
				 
				 prevfield=1;
			 
			 }//end of if
			  
			 ch=fgetc(fp1);
			 firstflag=0;

			 while(ch=='\n' || ch==' '|| ch=='\t')
			 {
			    firstflag=1;
				ch=fgetc(fp1);
			 }
			 
			 if(ch=='&')
			 {
				char nbsp[7];
				memset(nbsp,'\0',sizeof(nbsp));
				int nbin=0;
				while(strcmp(nbsp,"&nbsp")!=0)
				{
				  nbsp[nbin]=ch;
				  nbin++;
				  ch=fgetc(fp1);
				}//end while
				
				if(ch==';')
				 ch=fgetc(fp1);

			 }//end if
				
				
		  }//end of while

  
	   
	   if(*subtext!='\0')
	   {
		  if(prevfield==1)
		  {
			//fprintf(fp2,"<%s>\n",subfield);
			if(hypflag==1)
			{
			   fprintf(fp2,"<href>%s</href>\n",hyptext);
			   hypflag=0;
			}
			fprintf(fp2,"<value>%s</value>\n",subtext);
			
			memset(subtext,'\0',sizeof(subtext));
			subin=0;
			
			//fprintf(fp2,"</%s>\n",subfield);
			prevfield=0;

		  }
		  else
		  {
		   if(strcmp(subtext,"Click here to see all of them")!=0)
		   {
			 if(hypflag==1 )
			 {
			   fprintf(fp2,"<href>%s</href>\n",hyptext);
			   hypflag=0;
			 } 
			
			 fprintf(fp2,"<value>%s</value>\n",subtext);
		   }

		  }//end else 
		
	   }//end of if
	  }//end of if
		
								
	}//end of while

   //return if there is no table data
   if(endtag==1)
   {
	   fprintf(fp2,"</%s>\n",masttext);
	   return;
   }


	//skip two <tr> and parse from the third tr
	int cnttr=0;

	while(true)
	{
		ch=fgetc(fp1);
		
			
	  if(ch=='<')
	  {
		 memset(tagtext,'\0',150);
		 tagin=0;
		
		while(ch!='>')
		{
		   tagtext[tagin]=toupper(ch);
		   tagin++;
		   ch=fgetc(fp1);

		   while(ch=='\n' || ch==' '|| ch=='\t')
			  ch=fgetc(fp1);

		}
		if(strcmp(tagtext,"<TR")==0)
		  cnttr++;

	  
	  }
	  if(cnttr==3)
		  break;
	
	}//end of while true




	//start parsing the rows

  
   int counttd=0,tdflag=0;
   hypflag=0;

   int value=0;
   int row=1;
   while(true)
   {
		
	  ch=fgetc(fp1);
		
	  while(ch=='\n'||ch==' '|| ch=='\t')
	   ch=fgetc(fp1);

					
	  if(ch=='<')
	  {
		 memset(tagtext,'\0',150);
		 tagin=0;
		  
		 while(ch!='>')
		 {
		   tagtext[tagin]=toupper(ch);
	       tagin++;
		   ch=fgetc(fp1);

		 
		  if(strcmp(tagtext,"<TD")==0)
		  {
		   		
		   tdflag=1;
		   counttd++;
		  }
		 
		 }//end while
				 
		 
		 if(strcmp(tagtext,"</TR")==0)
		 {
			//print the submast tag of SNP
			fprintf(fp2,"</snp%d>\n",row);
			row++;
			counttd=0;
			tdflag=0;
		 }//end of if

		 //check for the end tag </table>
		 
		 if(strcmp(tagtext,"</TABLE")==0)
		 {
			 //end of the row
		    tablflag=0;
			 //fprintf(fp2,"</%s>\n",subfield);
			 //fprintf(fp2,"</%s>\n",masttext);
			break;
		 }		
			
	  }//end of if

	  if(tdflag==1)
	  {
		  switch(counttd)
		  {
		    case 1:
			 //row=1;
			 fprintf(fp2,"<snp%d>\n",row);
			 fprintf(fp2,"<snp_id>\n");
			 value=1;
			 break;

		    case 2:
			  fprintf(fp2,"<Contig_acesion>\n");
			  value=1;
			  break;
			 
			case 3:
			   fprintf(fp2,"<Position_in_contig>\n");
			   value=1;
			   break;
			 
			case 4:
			  fprintf(fp2,"<Strand>\n");
			  value=1;
			  break;
			 
			case 5:
			  fprintf(fp2,"<S5_flanking_sequence>\n");
			  value=1;
			  break;
			 
			case 6:
			  fprintf(fp2,"<S3_flanking_sequence>\n");
			  value=1;
			  break;
			case 7:
			  fprintf(fp2,"<Validation>\n");
			  value=1;
			  break;
			case 8:
			  fprintf(fp2,"<Nucl_change>\n");
			  value=1;
			  break;
			case 9:
			  fprintf(fp2,"<Amino_acid_change>\n");
			  value=1;
			  break;
			case 10:
			  fprintf(fp2,"<Type>\n");
			  value=1;
     		  break;

			case 11:
			  fprintf(fp2,"<mRna_accesion>\n");
			  value=1;
			  break;
			case 12:
			  fprintf(fp2,"<Protein_accesion>\n");
			  value=1;
			  break;
			case 13:
			  fprintf(fp2,"<Codon_position>\n");
			  value=1;
			  break;
			case 14:
			  fprintf(fp2,"<Position_in_protein>\n");
			  value=1;
			  break;

		
		   }//end of switch
		  
	  }//end of if
	  	  
	  	  
	  	  
	  if(tdflag==1)
	  {
		 fndlflag=0;
		while(true)
		{
		
		   if(fndlflag==0)
			ch=fgetc(fp1);
			
		   while(ch=='\n'||ch==' '|| ch=='\t')
			 ch=fgetc(fp1);
			
		   fndlflag=0;
			
		   if(ch=='<')
		   {
			  memset(tagtext,'\0',150);
		      tagin=0;
			  while(ch!='>')
			  {
			    tagtext[tagin]=toupper(ch);
			    tagin++;
			    ch=fgetc(fp1);
				 
				while(ch=='\n'||ch==' '|| ch=='\t')
				  ch=fgetc(fp1);

				if(strcmp(tagtext,"<AHREF=")==0)
				{
					memset(hyptext,'\0',150);
					hypin=0;
					ch=fgetc(fp1);
					hypflag=1;

					while(ch!='"')
					{
					  if(ch=='&')
					  {
						 hyptext[hypin]='$';
						 hypin++;

					  }
					  else
					  {
					    hyptext[hypin]=ch;
					    hypin++;
					    
					  }
					  ch=fgetc(fp1);

					}
					//fprintf(fp2,"<href>%s</href>\n",hyptext);
				
				}//end of if
						
				
			  }//end of while

			  if(strcmp(tagtext,"</TD")==0)
			  {
				
									
					switch(counttd)
					{
					  case 1:
						fprintf(fp2,"</snp_id>\n");
						break;
						
					  case 2:
						fprintf(fp2,"</Contig_acesion>\n");
						break;
			 
					  case 3:
						fprintf(fp2,"</Position_in_contig>\n");
						break;
			 
					  case 4:
						fprintf(fp2,"</Strand>\n");
						
						break;
			 
					  case 5:
						fprintf(fp2,"</S5_flanking_sequence>\n");
						
						break;
			 
					  case 6:
						fprintf(fp2,"</S3_flanking_sequence>\n");
														
						break;
					  case 7:
						fprintf(fp2,"</Validation>\n");
						
						break;
					  case 8:
						fprintf(fp2,"</Nucl_change>\n");
						
						break;
					  case 9:
						fprintf(fp2,"</Amino_acid_change>\n");
						
						break;
					  case 10:
						fprintf(fp2,"</Type>\n");
						
     					break;

					  case 11:
						fprintf(fp2,"</mRna_accesion>\n");
						
						break;
					  case 12:
						fprintf(fp2,"</Protein_accesion>\n");
						
						break;
					  case 13:
						fprintf(fp2,"</Codon_position>\n");
						
						break;
					  case 14:
						fprintf(fp2,"</Position_in_protein>\n");
						
						break;							
					  
											
					}//end of switch
				
				break;
						
			  }
			  
			  ch=fgetc(fp1);
			  fndlflag=1;
			  
			  
			  while(ch=='\n'|| ch==' '|| ch=='\t')
				 ch=fgetc(fp1);
			  

		}//end of if
			
			
			
	  if(ch!='<')
	  {
					    
		    memset(subtext,'\0',sizeof(subtext));
		    subin=0;
			
		  while(ch!='<')
		  {
			 
			  firstflag=0;
			  while(ch=='\n'|| ch==' '|| ch=='\t')
			  {
				  ch=fgetc(fp1);
				  firstflag=1;
			  }
			  
			  if(firstflag==1 && subin!=0)
			  {
			   subtext[subin]=' ';
			   subin++;
			  }
			  else
			  {
				subtext[subin]=ch;
			    subin++;
				
			  }
			
			  if(strcmp(subtext,"&nbsp")==0)
			  {
				
				if((ch=fgetc(fp1))!=';')
				  fseek(fp1,-1,SEEK_CUR);
				
				subin=subin-5;
				memset(&subtext[subin],'\0',5);

			  }//end if 

			 ch=fgetc(fp1);
				
		  }//end of while
			
			   		   
		   if(*subtext!='\0')
		   {
			     
			 fprintf(fp2,"<val%d>\n",value);
			 
			 if(hypflag==1)
			 {
				fprintf(fp2,"<href>%s</href>\n",hyptext);
				hypflag=0;
			 }
			  
			  fprintf(fp2,"<value>%s</value>\n",subtext);
			  fprintf(fp2,"</val%d>\n",value);
		      value++;

		   }//end of if
		   
		   fndlflag=1;
			  
			                 								
	  }//end of if
			
			
	 }//end of while true


	tdflag=0;

  }//end of if
		
		

  }//end of while true



  

  //extract 'All ncbi snp text
 int flflag=0;
 char ncbitext[20];
 int ncbin=0;
  while(true)
  {
      
	  if(flflag==0)
	   ch=fgetc(fp1);
	  flflag=0;
	  if(ch=='<')
	  {   
		  memset(tagtext,'\0',sizeof(tagtext));
		  tagin=0;
		  while(ch!='>')
		  {
			 tagtext[tagin]=toupper(ch);
			 tagin++;
			 ch=fgetc(fp1);
			 while(ch=='\n' ||ch=='\t'|| ch==' ')
				ch=fgetc(fp1);
			 
			 if(strcmp(tagtext,"<AHREF=")==0)
			 {
				ch=fgetc(fp1);
				while(ch=='\n' ||ch=='\t'|| ch==' ')
				  ch=fgetc(fp1);

				 memset(hyptext,'\0',sizeof(hyptext));
				 hypin=0;
				 while(ch!='"')
				 {
				   hyptext[hypin]=ch;
				   hypin++;
				   ch=fgetc(fp1);
				 }
				 hypflag=1;


			 }//end if
			 

		  }//end while
		  if(strcmp(tagtext,"</TR")==0)
		  {
			 fprintf(fp2,"</%s>\n",subfield);
			 fprintf(fp2,"</%s>\n",masttext);
			 break;
		  }
		 
		 ch=fgetc(fp1);
		 while(ch=='\n' ||ch=='\t'|| ch==' ')
		  ch=fgetc(fp1);
		 flflag=1;


	  }//end if

	  if(hypflag==1 && ch!='<')
	  {
		 memset(ncbitext,'\0',20);
		 ncbin=0;

		 while(ch!='<')
		 {
			 ncbitext[ncbin]=ch;
			 ncbin++;
			 ch=fgetc(fp1);

		 }
		 fprintf(fp2,"<href>%s</href>\n",hyptext);
		 fprintf(fp2,"<value>%s</value>\n",ncbitext);
		 flflag=1;


	  }//end of if


  }//end while true

 
  
  //fprintf(fp2,"</GENE>\n");
  //fclose(fp2);


  
 }//end of function


 void parsedisma4(FILE *fp1,FILE *fp2)
 {




    char tagtext[100],ch;
    int tagin,trflag=0;
    int endtr=0;
    int gwres=0;

	  //search for start tag <tr>
   while(true)
   {

     while(true)
	 {
		ch=fgetc(fp1);
		
		while(ch=='\n' || ch==' ' || ch=='\t')
		 ch=fgetc(fp1);

	   if(ch=='<')
	   {

		   memset(tagtext,'\0',100);
		   tagin=0;
		   while(ch!='>')
		   {
			  tagtext[tagin]=toupper(ch);
			  tagin++;
			  ch=fgetc(fp1);
			  while(ch=='\n' || ch==' ' || ch=='\t')
			   ch=fgetc(fp1);

			   //check for start tag
			  if(strcmp(tagtext,"<TR")==0)
			   trflag=1;

		   }//end while
		   if(trflag==1)
			  break;


	   }//end of if
	   /*else
	   {
		  ch=fgetc(fp1);
		  while(ch=='\n' || ch==' ' || ch=='\t')
			ch=fgetc(fp1);

	   }*/
	  
	 }//end of while




   //search for the next <td> and extract the text

	 //search for <td>
   int tdflag=0;

	 while(true)
	 {
		 ch=fgetc(fp1);
		 while(ch=='\n' || ch==' ' || ch=='\t')
		  ch=fgetc(fp1);


		if(ch=='<')
		{

		   memset(tagtext,'\0',100);
		   tagin=0;
		   while(ch!='>')
		   {
			   tagtext[tagin]=toupper(ch);
			   tagin++;
			   ch=fgetc(fp1);
			   while(ch=='\n' || ch==' ' || ch=='\t')
				ch=fgetc(fp1);

			   //check for start tag
			   if(strcmp(tagtext,"<TD")==0)
			    tdflag=1;

		   }
		   if(tdflag==1)
			   break;


		}//end if




	 }//end of while true

	 //extract the master text until <td> is found

	 
	 tdflag=0;
	 int firsttext=0,fndlflag,firstflag;
	 char masttext[100];
	 int masin=0;
	 memset(masttext,'\0',100);


	 fndlflag=0;
	 
	 while(true)
	 {
		 if(fndlflag==0)
		  ch=fgetc(fp1);

		 while(ch=='\n' || ch==' '|| ch=='\n')
			ch=fgetc(fp1);
           
		 fndlflag=0;

		 if(ch=='<')
		 {
             memset(tagtext,'\0',100);
			 tagin=0;
			 while(ch!='>')
			 {
			   tagtext[tagin]=toupper(ch);
			   tagin++;
			   ch=fgetc(fp1);

			   while(ch=='\n' || ch==' '|| ch=='\n')
			     ch=fgetc(fp1);
				
			   if(strcmp(tagtext,"<TD")==0)
				tdflag=1;


			 }//end while
			 if(tdflag==1)
				 break;

			 ch=fgetc(fp1);
			 fndlflag=1;

			 while(ch=='\n' || ch==' '|| ch=='\n')
			      ch=fgetc(fp1);


		 }


		if(ch!='<')
		{
		  masin=0; 
		  while(ch!='<')
		  {
			 
			 firstflag=0;

			 while(ch=='\n' || ch==' '|| ch=='\t')
			 {
		      ch=fgetc(fp1);
		      firstflag=1;

			 }
			
			//extract the master text 
		   if(firsttext==0)
		   {
		     
			 if((firstflag==1 && masin!=0) || ch=='&')
			 {
		      masttext[masin]='_';
		      masin++;

			 }
			 else
			 {
			  if(!(ch==':' || ch==','))
			  {
			   masttext[masin]=toupper(ch);
		       masin++;
			  }
			 }
			 
		   }//end of if
			  
		  if(firstflag==0)
		    ch=fgetc(fp1);


		}//end of while
		firsttext=1;
	 

		}//end if


	 }//end of while


	 //put the master text in the file
	 fprintf(fp2,"<%s>\n",masttext);
	 fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);


    char subtext[1500],subfield[100],hyptext[350],ssubfield[100],buttext[50],valtext[7],actlink[300],acttext[8];
    int hypin,hypflag=0,bullhead=0;
    int subin,tsubin,sfin,ssfin;

    memset(subfield,'\0',100);
    memset(ssubfield,'\0',100);
    memset(subtext,'\0',sizeof(subtext));
    memset(buttext,'\0',50);
    subin=0;
    int butin,actin,valin;
    int disval=0,sdisval=0,raflag=0,raval=0;
    int swissflag=1;
    int endtag=0;
    
  //extract the subfields if any

	 fndlflag=0;

	 while(true)
	 {
		if(fndlflag==0)
		 ch=fgetc(fp1);
		
		while(ch=='\n'||ch==' '|| ch=='\t')
		  ch=fgetc(fp1);
					 
		if(ch=='<')
		{
		  memset(tagtext,'\0',100);
		  tagin=0;

		  while(ch!='>')
		  {
			  tagtext[tagin]=toupper(ch);
			  tagin++;
			  ch=fgetc(fp1);
			  while(ch=='\n'||ch==' '|| ch=='\t')
				ch=fgetc(fp1);

			  if(strcmp(tagtext,"<AHREF=")==0)
			  {
				  ch=fgetc(fp1);
				  while(ch=='\n'||ch==' '|| ch=='\t')
				   ch=fgetc(fp1);

				  memset(hyptext,'\0',350);
				  hypin=0;
				  while(ch!='"')
				  {
					  if(ch=='&')
					  {
						hyptext[hypin]='$';
						hypin++;
					  }
					  else
					  {
					   hyptext[hypin]=ch;
					   hypin++;
					  }
					  
					  ch=fgetc(fp1);
					  while(ch=='\n'||ch==' '|| ch=='\t')
						ch=fgetc(fp1);

				  }
				  hypflag=1;

			  }//end if


			  if(strcmp(tagtext,"<FORMMETHOD=")==0)
			  {
				  ch=fgetc(fp1);

				  while(ch=='\n'||ch==' '|| ch=='\t')
				    ch=fgetc(fp1);
				  while(ch!='"')
				    ch=fgetc(fp1);

				  ch=fgetc(fp1);
				  				  
				  memset(acttext,'\0',8);
				  actin=0;
				  
				  while(strcmp(acttext,"ACTION=")!=0)
				  {
					 ch=fgetc(fp1);
					  
					while(ch=='\n' || ch==' ' || ch=='\t')
					   ch=fgetc(fp1);
					   
					 acttext[actin]=toupper(ch);
					 actin++;

				  }//end while

				  ch=fgetc(fp1);
				  while(ch=='\n' || ch==' ' || ch=='\t')
				   ch=fgetc(fp1);

				  ch=fgetc(fp1);
				  memset(actlink,'\0',300);
				  actin=0;

				  while(ch!='"')
				  {
					
					if(ch=='&')
					{
					 actlink[actin]='_';
					 actin++;
					}
					else
					{
					 actlink[actin]=ch;
					 actin++;
					}
					ch=fgetc(fp1);
					while(ch=='\n' || ch==' ' || ch=='\t')
					 ch=fgetc(fp1);  

				  }

				  //search for the text value
				  ch=fgetc(fp1);
				  memset(valtext,'\0',6);
				  valin=0;
				  					
				  while(strcmp(valtext,"VALUE")!=0)
				  {
					ch=fgetc(fp1);
					if(ch==' ' || valin>4)
					{
					  memset(valtext,'\0',6);
					  valin=0;
					}
					if(ch==' ')
					{
					  while(ch==' ')
					   ch=fgetc(fp1);
					}

					valtext[valin]=toupper(ch);
					valin++;
									
							
				  }//end while
                  
				  while(ch!='"')
				  {
					ch=fgetc(fp1);
					while(ch=='\n' || ch==' ' || ch=='\t')
                     ch=fgetc(fp1);
				  }
      
		   //extract the value of the button
				  
				  ch=fgetc(fp1);
				  while(ch=='\n' || ch==' ' || ch=='\t')
                    ch=fgetc(fp1);

				  memset(buttext,'\0',50);
				  butin=0;
				  while(ch!='"')
				  {
					
					if(ch==' ')
					{
					  buttext[butin]='_';
					  butin++;
					}
					else
					{
					   buttext[butin]=toupper(ch);
					   butin++;
					}
					ch=fgetc(fp1);

				  }//end while

			  //put the text and the link in the file
				  
				  fprintf(fp2,"<%s>\n",buttext);
				  fprintf(fp2,"<href>%s</href>\n",actlink);
				  	

			  }//end if
			  
			  if(strcmp(tagtext,"</TR")==0)
			  {
				 endtag=1;
				 endtr++;
			  }
			  
			  if(strcmp(tagtext,"<LI")==0)
				  bullhead=1;


		  }//end of while
		  
		  if(endtag==1)
		  {
		    if(*buttext!=0)
		     fprintf(fp2,"</%s>\n",buttext);
			if(strcmp(masttext,"RESEARCH_ARTICLES")==0)
				gwres=1;

			if(*subfield!='\0')
			  fprintf(fp2,"</%s>\n",subfield);	
		    
			fprintf(fp2,"</%s>\n",masttext);
			break;
	 
		  }     
		
		  		  
		   ch=fgetc(fp1);
		  
		   while(ch=='\n'||ch==' '|| ch=='\t')
			ch=fgetc(fp1);
		     
		    if(ch=='&')
			{
			   char nbsp[7];
			   memset(nbsp,'\0',sizeof(nbsp));
			   int nbin=0;
			   while(strcmp(nbsp,"&nbsp")!=0)
			   {
				 nbsp[nbin]=ch;
				 nbin++;
				 ch=fgetc(fp1);
			   }//end while
				
			   if(ch==';')
				ch=fgetc(fp1);

			 }//end if
		  
		    fndlflag=1;

		}//end if
					

	    if(ch!='<')
		{
		 
		  fndlflag=1;
		 
		  if(bullhead==0 && swissflag==1)
		 {
			firstflag=0;
			//memset(subtext,'\0',350);
			//subin=0;
			
			while(ch!='<')
			{
				
			  if(firstflag==1  && subin!=0)
			  {
				subtext[subin]=' ';
				subin++;
			  }
			  
			    subtext[subin]=ch;
				subin++;

			  			
			   firstflag=0;
				
			   if(ch==':' &&  strcmp(subtext,"search databases for MIM named disorders:")!=0)
			   {
				 if(*subfield!='\0' && gwres==0)
				  fprintf(fp2,"</%s>\n",subfield);
				   
				   memset(subfield,'\0',100);
				   tsubin=0;
				   sfin=0;
				   while(subtext[tsubin]!=':')
				   {
					 if(subtext[tsubin]==' ')
					 {
						subfield[sfin]='_';
						sfin++;
						tsubin++;

					 }
					 else
					 {					 
					   subfield[sfin]=subtext[tsubin];
					   sfin++;
					   tsubin++;
					 }

				   }
				   
				   memset(subtext,'\0',sizeof(subtext));
				   subin=0;
				
			   }//end if
			   
			  if(strcmp(subtext,"search databases for MIM named disorders:")==0)
			  {
				  memset(subtext,'\0',sizeof(subtext));
				  subin=0;
			  }
			  		
			  	            
			   ch=fgetc(fp1);
       				
			   while(ch=='\n'||ch==' '|| ch=='\t')
			   {
				  firstflag=1;
				  ch=fgetc(fp1);
				}
			  
			   if(strcmp(subtext,"&nbsp")==0)
			   {
				memset(subtext,'\0',sizeof(subtext));
				subin=0;
				if(ch==';')
				  ch=fgetc(fp1);
							 
			   }
			    
			   if(ch=='&')
			   {
				 char nbsp[7];
				 memset(nbsp,'\0',sizeof(nbsp));
				 int nbin=0;
				 while(strcmp(nbsp,"&nbsp")!=0)
				 {
				  nbsp[nbin]=ch;
				  nbin++;
				  ch=fgetc(fp1);
				 }//end while
				
				 if(ch==';')
				  ch=fgetc(fp1);

			   }//end if
			
				

		   }//end while
			
		  if(strcmp(subtext,"Human Gene Mutation Database")==0)
		  {
			 if(*subfield!='\0')
			   fprintf(fp2,"</%s>\n",subfield);
			  memset(subfield,'\0',sizeof(subfield));
		  }


		  if(*subtext!='\0')
		  {
			if(*subfield!='\0')
			{
			  fprintf(fp2,"<%s>\n",subfield);
			   
			  if(hypflag==1)
			  {
				fprintf(fp2,"<href>%s</href>\n",hyptext);
				hypflag=0;
				memset(hyptext,'\0',350);
			  }
			  fprintf(fp2,"<value>%s</value>\n",subtext);
			   
			  if(gwres==1)
			  {
			   fprintf(fp2,"</%s>\n",subfield);
			   memset(subfield,'\0',sizeof(subfield));
			  }

			  memset(subtext,'\0',sizeof(subtext));
			  subin=0;

			  if(strcmp(subfield,"GeneClinics")==0)
			  {
				 if(*subfield!='\0')
				 {
				  fprintf(fp2,"</%s>\n",subfield);
				  memset(subfield,'\0',sizeof(subfield));
				 }
				 
			  }//end if

			}//end if
			else
			{
			   if(hypflag==1)
			   {
				 fprintf(fp2,"<href>%s</href>\n",hyptext);
				 hypflag=0;
				 memset(hyptext,'\0',350);
			   }
			   fprintf(fp2,"<value>%s</value>\n",subtext);
			   memset(subtext,'\0',350);
			   subin=0;

			}//end else

		   }//end if
		 
		 }//end if


		   //bullhead text
		  if(bullhead==1)
		  {
			 swissflag=0;
			 //memset(subtext,'\0',350);
			 //subin=0;
             firstflag=0;
			
			while(ch!='<')
			{
				
			  if(firstflag==1  && subin!=0)
			  {
				subtext[subin]=' ';
				subin++;
			  }
			  
			    subtext[subin]=ch;
				subin++;
		   
				firstflag=0;
				
			  if(ch==':' && strcmp(subfield,"SWISS-PROT")==0)
			  {
				 memset(ssubfield,'\0',100);
				 tsubin=0;
				 ssfin=0;
				 while(subtext[tsubin]!=':')
				 {
				   ssubfield[ssfin]=subtext[tsubin];
				   ssfin++;
				   tsubin++;
                  
				 }
				 
				 //fprintf(fp2,"<%s>\n",subfield);
				 
				 memset(subtext,'\0',sizeof(subtext));
				 subin=0;
				
			  }//end if
				
				ch=fgetc(fp1);
				
				while(ch=='\n'||ch==' '|| ch=='\t')
				{
				   firstflag=1;
				   ch=fgetc(fp1);
				}



			}//end while

			if(*subtext!=0 && (strcmp(masttext,"RESEARCH_ARTICLES")==0 || strcmp(masttext,"MEDICAL_NEWS")==0))
			{
				raval++;
				if(hypflag==1)
				{
					fprintf(fp2,"<href>%s</href>\n",hyptext);
					hypflag=0;
				}
				
				//if(*buttext==0 && strcmp(masttext,"RESEARCH_ARTICLES")==0)
				 //fprintf(fp2,"<ra%d>\n<value>%s</value>\n</ra%d>\n",raval,subtext,raval);
				//else
				fprintf(fp2,"<value>%s</value>\n",subtext);	
				raflag=1;
				memset(subtext,'\0',350);
				subin=0;
			}

			if(*subtext!=0 && strcmp(subfield,"SWISS-PROT")!=0 && raflag==0)
			{
			  			  
			  disval++;
			  fprintf(fp2,"<disease%d>%s</disease%d>\n",disval,subtext,disval);
			  bullhead=0;
			   			  
			  swissflag=1;
			  memset(subtext,'\0',sizeof(subtext));
			  subin=0;


			}//end if
			if(*ssubfield!='\0')
			{
			   sdisval++;
			   
			   fprintf(fp2,"<%s%d>%s</%s%d>\n",ssubfield,sdisval,subtext,ssubfield,sdisval);
			   bullhead=0;
			   memset(ssubfield,'\0',100);
			   
			   memset(subtext,'\0',350);
			   subin=0;
			   swissflag=1;

			}//end if

		  
		  }//end if bullhead

		  
	  }//end if


	 }//end of while
	 
	 	 
	 if(endtr==5)
	 {
		 //fprintf(fp2,"</%s>\n",masttext);
		 break;
	 }
	
  }//end of while

   
   //fprintf(fp2,"</GENE>\n");
   //fclose(fp2);
    


  }//end of function





 void parsespdat(FILE *fp1,FILE *fp2)
 {


	 char tagtext[100],ch;
	 int tagin,trflag=0;
	 
	 while(true)
	 {
		
		 ch=fgetc(fp1);
		
		while(ch=='\n' || ch==' ' || ch=='\t')
		 ch=fgetc(fp1);

	    if(ch=='<')
		{

		  memset(tagtext,'\0',100);
		  tagin=0;
		  while(ch!='>')
		  {
			 tagtext[tagin]=toupper(ch);
			 tagin++;
			 ch=fgetc(fp1);
			 while(ch=='\n' || ch==' ' || ch=='\t')
			  ch=fgetc(fp1);

			   //check for start tag
			  if(strcmp(tagtext,"<TR")==0)
			   trflag=1;

		  }//end while
		  if(trflag==1)
		    break;
		
		}//end of if
	  
	  
	 
	 }//end of while true




   //search for the next <td> and extract the text

	 //search for <td>
   int tdflag=0;

	 while(true)
	 {
		 ch=fgetc(fp1);
		 while(ch=='\n' || ch==' ' || ch=='\t')
		  ch=fgetc(fp1);


		if(ch=='<')
		{

		   memset(tagtext,'\0',100);
		   tagin=0;
		   while(ch!='>')
		   {
			   tagtext[tagin]=toupper(ch);
			   tagin++;
			   ch=fgetc(fp1);
			   while(ch=='\n' || ch==' ' || ch=='\t')
				ch=fgetc(fp1);

			   //check for start tag
			   if(strcmp(tagtext,"<TD")==0)
			    tdflag=1;

		   }
		   if(tdflag==1)
			   break;


		}//end if


	 }//end of while true

	 //extract the master text until <td> is found
	 		 
	 tdflag=0;
	 int firsttext=0,flflag,firstflag;
	 char masttext[100];
	 int masin=0;
	 memset(masttext,'\0',sizeof(masttext));


	 flflag=0;
	 
	 while(true)
	 {
		 if(flflag==0)
		  ch=fgetc(fp1);

		 while(ch=='\n' || ch==' '|| ch=='\n')
			ch=fgetc(fp1);
           
		 flflag=0;

		 if(ch=='<')
		 {
             memset(tagtext,'\0',100);
			 tagin=0;
			 while(ch!='>')
			 {
				tagtext[tagin]=toupper(ch);
				tagin++;
				ch=fgetc(fp1);

				while(ch=='\n' || ch==' '|| ch=='\n')
			       ch=fgetc(fp1);
				
				if(strcmp(tagtext,"<TD")==0)
					tdflag=1;


			 }
			 if(tdflag==1)
				 break;

			 ch=fgetc(fp1);
			 flflag=1;

			 while(ch=='\n' || ch==' '|| ch=='\n')
			    ch=fgetc(fp1);

		 }//end if


	   if(ch!='<')
	   {
		 masin=0; 
		 while(ch!='<')
		 {
			 
		   firstflag=0;

		    while(ch=='\n' || ch==' '|| ch=='\t')
			{
		      ch=fgetc(fp1);
		      firstflag=1;

			}//end while
			
			//extract the master text 
		   if(firsttext==0)
		   {
		     
			 if((firstflag==1 && masin!=0) || ch=='&')
			 {
		      masttext[masin]='_';
		      masin++;

			 }
			 else
			 {
			  if(!(ch==':' || ch==','))
			  {
			   masttext[masin]=toupper(ch);
		       masin++;
			  }
			 }
			 
		   }//end of if
			  
		  if(firstflag==0)
		    ch=fgetc(fp1);


		}//end of while
		firsttext=1;
	 

	   }//end if


	 }//end of while


	 //put the master text in the file
	 fprintf(fp2,"<%s>\n",masttext);
	 fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);


	 //search for the tag table

    int tablflag=0;
	int endtag=0;

    while(true)
	{
	  
	   ch=fgetc(fp1);
	   
	   while(ch=='\n' || ch==' '|| ch=='\t')
	    ch=fgetc(fp1);
	
	  //flflag=0;

	  if(ch=='<')
	  {
		  memset(tagtext,'\0',sizeof(tagtext));
		  tagin=0;
		   
		 while(ch!='>')
		 {
		   tagtext[tagin]=toupper(ch);
		   tagin++;
		   ch=fgetc(fp1);
		   
		   while(ch=='\n' || ch==' '|| ch=='\t')
			 ch=fgetc(fp1);

		   if(strcmp(tagtext,"<TABLE")==0)
		   {
			   
			  tablflag=1;

		   }
		   if(strcmp(tagtext,"</TR")==0)
		   {
			  fprintf(fp2,"</%s>\n",masttext);			   				
			  endtag=1;
		   }

		   
		  
		 }//end of while
	 
	  
	  }//end if
	  if(tablflag==1)
		break;
	  if(endtag==1)
		return;
	
	}//end while true


	//check for tr and parse from the 2nd tr
    
	int cnttr=0;

	while(true)
	{
	   ch=fgetc(fp1);
	   while(ch=='\n' || ch==' '|| ch=='\t')
		ch=fgetc(fp1);
		
			
	   if(ch=='<')
	   {
		 memset(tagtext,'\0',sizeof(tagtext));
		 tagin=0;
		
		while(ch!='>')
		{
		   tagtext[tagin]=toupper(ch);
		   tagin++;
		   ch=fgetc(fp1);

		   while(ch=='\n' || ch==' '|| ch=='\t')
			  ch=fgetc(fp1);
		
		  if(strcmp(tagtext,"<TR")==0)
		    cnttr++;
		}

	  
	   }//end if
	  if(cnttr==2)
		 break;
	
	}//end of while true

   int counttd=0;


   flflag=0;
   char subtext[900],subfield[150],hyptext[200];
   int subin,sfin,hypin,hypflag=0;;
   int fstflag=0,endtab=0,bullhead=0;
   memset(subfield,'\0',sizeof(subfield));
   memset(subtext,'\0',sizeof(subtext));
   subin=0;
   tdflag=0;

   while(true)
   {
	  
	   if(flflag==0)
	    ch=fgetc(fp1);

	   while(ch=='\n'||ch==' ' ||ch=='\t')
		  ch=fgetc(fp1);

	   if(ch=='<')
	   {
		  memset(tagtext,'\0',sizeof(tagtext));
		  tagin=0;
		  while(ch!='>')
		  {
		     tagtext[tagin]=toupper(ch);
			 tagin++;
			 ch=fgetc(fp1);

			 while(ch=='\n'||ch==' ' ||ch=='\t')
			  ch=fgetc(fp1);
			 
			 if(strcmp(tagtext,"<TD")==0)
			 {
				//if(counttd==1)
				 //fprintf(fp2,"</%s>\n",subfield);
				 
				
				tdflag=1;
				counttd++;
				if(counttd==2)
                  fprintf(fp2,"<description>\n");
			 }
			 if(strcmp(tagtext,"</TD")==0)
			 {
				 if(counttd==2)
                  fprintf(fp2,"</description>\n");

			 }
			 
			 if(strcmp(tagtext,"</TR")==0)
			 {
								  
				
				bullhead=0;
				if(*subfield!='\0') 
				 fprintf(fp2,"</%s>\n",subfield);
				
				memset(subfield,'\0',sizeof(subfield));
				fstflag=0;
				 
				counttd=0;
			 }//end if
			 
			 if(strcmp(tagtext,"</TABLE")==0)
			 {
				fprintf(fp2,"</%s>\n",masttext);
				endtab=1;

			 }//end if
			 if(strcmp(tagtext,"<LI")==0)
			 {
               bullhead=1;
			 }

			 if(strcmp(tagtext,"<AHREF=")==0)
			 {
			    ch=fgetc(fp1);
				while(ch=='\n'||ch==' ' ||ch=='\t')
			     ch=fgetc(fp1);
				 				 
				memset(hyptext,'\0',sizeof(hyptext));
				hypin=0;

				while(ch!='"')
				{
				  if(ch=='&')
				  {
					hyptext[hypin]='$';
					hypin++;
				  }
			      else
				  {				 
					hyptext[hypin]=ch;
					hypin++;
				  }
				  ch=fgetc(fp1);
				  while(ch=='\n'||ch==' ' ||ch=='\t')
			       ch=fgetc(fp1);


				}//end while
				hypflag=1;

			 }//end if

		
		   }//end while
		  
		  if(endtab==1)
			break;

		  ch=fgetc(fp1);
		  while(ch=='\n'||ch==' ' ||ch=='\t')
		  {
		    ch=fgetc(fp1);
			firstflag=1;
		  }
		  flflag=1;
	   
	   }//end if


           

      if(ch!='<')
	  {
		 flflag=1;
		 

		while(ch!='<')
		{
			
		  if(firstflag==1 && subin!=0)
		  {
		    subtext[subin]=' ';
			subin++;
		  }
		  subtext[subin]=ch;
		  subin++;
			
		  
		  
		   ch=fgetc(fp1);
		   firstflag=0;
		   while(ch=='\n'||ch==' ' ||ch=='\t')
		   {
		     ch=fgetc(fp1);
			 firstflag=1;
		   }

		}//end while
	      if(/*ch==':'*/ hypflag==1)
		  {
			memset(subfield,'\0',sizeof(subfield));
			sfin=0;
			subin=0;
			while(subtext[subin]!='\0')
			{
			  if(subtext[subin]==' ')
				 subfield[sfin]='_';
			   else
				 subfield[sfin]=subtext[subin];

				 subin++;
				 sfin++;
									
			}//end while
			fprintf(fp2,"<%s>\n",subfield);
			memset(subtext,'\0',sizeof(subtext));
			subin=0;
			//fstflag=1;

		  }//end if

	    if(/**subtext!=0 && fstflag==1 &&*/ bullhead==0)
		{
		  if(hypflag==1)
		  {
		   fprintf(fp2,"<href>%s</href>\n",hyptext);
		   hypflag=0;
		  }
		  if(*subtext!='\0')
		   fprintf(fp2,"<value>%s</value>\n",subtext);
		  memset(subtext,'\0',sizeof(subtext));
		  subin=0;

		}//end if



	  }//end if

	  
   
    }//end while true


 

  

 }//end function

 void parseservices(FILE *fp1,FILE *fp2)
 {
	 char tagtext[100],ch;
	 int tagin,trflag=0;
	 
	 while(true)
	 {
		
		 ch=fgetc(fp1);
		
		while(ch=='\n' || ch==' ' || ch=='\t')
		 ch=fgetc(fp1);

	    if(ch=='<')
		{

		  memset(tagtext,'\0',100);
		  tagin=0;
		  while(ch!='>')
		  {
			 tagtext[tagin]=toupper(ch);
			 tagin++;
			 ch=fgetc(fp1);
			 while(ch=='\n' || ch==' ' || ch=='\t')
			  ch=fgetc(fp1);

			   //check for start tag
			  if(strcmp(tagtext,"<TR")==0)
			   trflag=1;

		  }//end while
		  if(trflag==1)
		    break;
		
		}//end of if
	  
	  
	 
	 }//end of while true




   //search for the next <td> and extract the text

	 //search for <td>
   int tdflag=0;

	 while(true)
	 {
		 ch=fgetc(fp1);
		 while(ch=='\n' || ch==' ' || ch=='\t')
		  ch=fgetc(fp1);


		if(ch=='<')
		{

		   memset(tagtext,'\0',100);
		   tagin=0;
		   while(ch!='>')
		   {
			   tagtext[tagin]=toupper(ch);
			   tagin++;
			   ch=fgetc(fp1);
			   while(ch=='\n' || ch==' ' || ch=='\t')
				ch=fgetc(fp1);

			   //check for start tag
			   if(strcmp(tagtext,"<TD")==0)
			    tdflag=1;

		   }
		   if(tdflag==1)
			   break;


		}//end if


	 }//end of while true

	 //extract the master text until <td> is found
	 		 
	 tdflag=0;
	 int firsttext=0,flflag,firstflag;
	 char masttext[100];
	 int masin=0;
	 memset(masttext,'\0',sizeof(masttext));


	 flflag=0;
	 
	 while(true)
	 {
		 if(flflag==0)
		  ch=fgetc(fp1);

		 while(ch=='\n' || ch==' '|| ch=='\n')
			ch=fgetc(fp1);
           
		 flflag=0;

		 if(ch=='<')
		 {
             memset(tagtext,'\0',100);
			 tagin=0;
			 while(ch!='>')
			 {
				tagtext[tagin]=toupper(ch);
				tagin++;
				ch=fgetc(fp1);

				while(ch=='\n' || ch==' '|| ch=='\n')
			       ch=fgetc(fp1);
				
				if(strcmp(tagtext,"<TD")==0)
					tdflag=1;


			 }
			 if(tdflag==1)
				 break;

			 ch=fgetc(fp1);
			 flflag=1;

			 while(ch=='\n' || ch==' '|| ch=='\n')
			    ch=fgetc(fp1);

		 }//end if


	   if(ch!='<')
	   {
		 masin=0; 
		 while(ch!='<')
		 {
			 
		   firstflag=0;

		    while(ch=='\n' || ch==' '|| ch=='\t')
			{
		      ch=fgetc(fp1);
		      firstflag=1;

			}//end while
			
			//extract the master text 
		   if(firsttext==0)
		   {
		     
			 if((firstflag==1 && masin!=0) || ch=='&')
			 {
		      masttext[masin]='_';
		      masin++;

			 }
			 else
			 {
			  if(!(ch==':' || ch==','))
			  {
			   masttext[masin]=toupper(ch);
		       masin++;
			  }
			 }
			 
		   }//end of if
			  
		  if(firstflag==0)
		    ch=fgetc(fp1);


		}//end of while
		firsttext=1;
	 

	   }//end if


	 }//end of while


	 //put the master text in the file
	 fprintf(fp2,"<%s>\n",masttext);
	 fprintf(fp2," <gid>\n  <value>%s</value>\n </gid>\n",genid);


	 //search for the tag table

    int tablflag=0;
	int endtag=0;

    while(true)
	{
	  
	   ch=fgetc(fp1);
	   
	   while(ch=='\n' || ch==' '|| ch=='\t')
	    ch=fgetc(fp1);
	
	  //flflag=0;

	  if(ch=='<')
	  {
		  memset(tagtext,'\0',sizeof(tagtext));
		  tagin=0;
		   
		 while(ch!='>')
		 {
		   tagtext[tagin]=toupper(ch);
		   tagin++;
		   ch=fgetc(fp1);
		   
		   while(ch=='\n' || ch==' '|| ch=='\t')
			 ch=fgetc(fp1);

		   if(strcmp(tagtext,"<TABLE")==0)
		   {
			   
			  tablflag=1;

		   }
		   if(strcmp(tagtext,"</TR")==0)
		   {
			  fprintf(fp2,"</%s>\n",masttext);			   				
			  endtag=1;
		   }
		   
		  
		 }//end of while
	 
	  
	  }//end if
	  if(tablflag==1)
		break;
	  if(endtag==1)
	  {
		fprintf(fp2,"</GENE>\n");
		fclose(fp2);		
 		 return;
	  }
	
	}//end while true


	//check for tr and parse from the 2nd tr
    
	int cnttr=0;

	while(true)
	{
	   ch=fgetc(fp1);
	   while(ch=='\n' || ch==' '|| ch=='\t')
		ch=fgetc(fp1);
		
			
	   if(ch=='<')
	   {
		 memset(tagtext,'\0',sizeof(tagtext));
		 tagin=0;
		
		while(ch!='>')
		{
		   tagtext[tagin]=toupper(ch);
		   tagin++;
		   ch=fgetc(fp1);

		   while(ch=='\n' || ch==' '|| ch=='\t')
			  ch=fgetc(fp1);
		
		  if(strcmp(tagtext,"<TR")==0)
		    cnttr++;
		}

	  
	   }//end if
	  if(cnttr==1)
		 break;
	
	}//end of while true

   //int counttd=0;


   flflag=0;
   char subtext[300],subfield[150],hyptext[200];
   int subin,sfin,hypin,hypflag=0;;
   int fstflag=0,endtab=0;
   memset(subtext,'\0',sizeof(subtext));
   subin=0;
   tdflag=0;

   while(true)
   {
	  
	   if(flflag==0)
	    ch=fgetc(fp1);

	   while(ch=='\n'||ch==' ' ||ch=='\t')
		  ch=fgetc(fp1);

	   if(ch=='<')
	   {
		  memset(tagtext,'\0',sizeof(tagtext));
		  tagin=0;
		  while(ch!='>')
		  {
		     tagtext[tagin]=toupper(ch);
			 tagin++;
			 ch=fgetc(fp1);

			 while(ch=='\n'||ch==' ' ||ch=='\t')
			  ch=fgetc(fp1);
			 
			 /*if(strcmp(tagtext,"<TD")==0)
			 {

			 }
			 if(strcmp(tagtext,"</TD")==0)
			 {
				 

			 }*/
			 
			 if(strcmp(tagtext,"</TR")==0)
			 {
				fprintf(fp2,"</%s>\n",subfield);
								  
								
			 }//end if
			 
			 if(strcmp(tagtext,"</TABLE")==0)
			 {
				fprintf(fp2,"</%s>\n",masttext);
				endtab=1;

			 }//end if

			 if(strcmp(tagtext,"<AHREF=")==0)
			 {
				 ch=fgetc(fp1);
				 while(ch=='\n'||ch==' ' ||ch=='\t')
			      ch=fgetc(fp1);
				 				 
				 memset(hyptext,'\0',sizeof(hyptext));
				 hypin=0;

				 while(ch!='"')
				 {
					if(ch=='&')
					{
					  hyptext[hypin]='$';
					  hypin++;
					}
			        else
					{
						
					 hyptext[hypin]=ch;
					 hypin++;
					}
					ch=fgetc(fp1);
				    while(ch=='\n'||ch==' ' ||ch=='\t')
			         ch=fgetc(fp1);


				 }//end while
				 hypflag=1;

			 }//end if

		
		   }//end while
		  if(endtab==1)
			break;

		  ch=fgetc(fp1);
		  while(ch=='\n'||ch==' ' ||ch=='\t')
		  {
		    ch=fgetc(fp1);
			firstflag=1;
		  }
		  flflag=1;
	   
	   }//end if

           

      if(ch!='<')
	  {
		 flflag=1;
		 
		while(ch!='<')
		{
			
			if(firstflag==1 && subin!=0)
			{
			  subtext[subin]=' ';
			  subin++;
			}
			
			subtext[subin]=ch;
			subin++;
					   
		    ch=fgetc(fp1);
		    firstflag=0;
		   while(ch=='\n'||ch==' ' ||ch=='\t')
		   {
		     ch=fgetc(fp1);
			 firstflag=1;
		   }

		}//end while
		
		  
	  }//end if

	  if(*subtext!=0)
	  {
		  if(hypflag==1)
		  {
			 subin=0;
			 sfin=0;
			 memset(subfield,'\0',sizeof(subfield));
			 
			 while(subtext[subin]!='\0')
			 {
				if(subtext[sfin]==' ')
				 subfield[sfin]='_';
				else
				 subfield[sfin]=subtext[subin];
				sfin++;
				subin++;

			 }//end while

			  
			 fprintf(fp2,"<%s>\n",subfield);
			 fprintf(fp2,"<href>%s</href>\n",hyptext);
			 hypflag=0;
		     memset(subtext,'\0',sizeof(subtext));
		     subin=0;
		  }
		  else
		  {
			fprintf(fp2,"<value>%s</value>\n",subtext);
			memset(subtext,'\0',sizeof(subtext));
		    subin=0;
		  }

	  }//end if


   }//end while true

   fprintf(fp2,"</GENE>");
   fclose(fp2);

 }//end function











void main()
{

   //extract all the files
    FILE *ffile;
	char ftext[100];
	char che;
	
	cout<<"Enter the file names which contain the files:";
	cout.flush();
	cin.getline(ftext,100,'\n');

	ffile=fopen(ftext,"r");
	if(ffile==NULL)
	{
	  cout<<"file doesn't exist"<<endl;
	  exit(1);
	}
	che=fgetc(ffile);
	
	char filename[100];
	char tfile[20];
	FILE *fp1,*fp2;
	int tin;
	char xmlfile[40];
	
	char *gencard;

  while(che!=EOF)
  {
       
    
	memset(filename,'\0',sizeof(filename));
	memset(tfile,'\0',sizeof(tfile));
	
    
	//char ch;


	/*cout<<"Enter the genecard file name:";
	cout.flush();
	cin.getline(filename,100,'\n');*/
     
	 tin=0;
     
	 while(che!='\n')
	 {
	    tfile[tin]=che;
		tin++;
		che=fgetc(ffile);
	 
	 }//end while

	 strcpy(filename,"c:\\cancergene\\canc\\");
	 strcat(filename,tfile);

	 fp1=fopen(filename,"r");

	 if(fp1==NULL)
	 {
	   cout<<"File doesn't exist"<<endl;
	   exit(1);
	 }

	
   //ch=fgetc(fp1);
   	
   
   //get the gene id
   
	//ch=fgetc(fp1);
	 gencard=Genid(fp1);
	
	 int lengc=strlen(gencard);
	
	 for(int j=lengc;gencard[j]!=' ';j--);
	
	 j++;
	 int i=0;
	 memset(genid,'\0',15);

	while(gencard[j]!='\0')
	{
	  genid[i]=gencard[j];
	  i++;
	  j++;
	}//end of while
	
    cout<<genid<<endl;
    
	strcpy(xmlfile,"d:\\gcxml\\canc\\");
	strcat(xmlfile,genid);
	strcat(xmlfile,".xml");

    fp2=fopen(xmlfile,"w");
	
	fprintf(fp2,"<GENE>\n");

    parsef4row(fp1,fp2);  
    parseexpr(fp1,fp2);
	  
	parsesimgene(fp1,fp2);
 	parseSnp(fp1,fp2);
	parsedisma4(fp1,fp2);
	parsespdat(fp1,fp2);
	parseservices(fp1,fp2);
	

	che=fgetc(ffile);
	fclose(fp1);
	
	//if(che==EOF)
	 //cout<<"";
  
 
  }//end while

	   
	   
 }//end of main









 

